package com.leaf.controller;


import com.leaf.service.impl.ISimpleTestJRuby;
import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;

/**
 * Created by yinZheng on 16-3-2.
 */
@Controller
@RequestMapping("springTest")
@SessionAttributes("testUser")
public class SpringTestController {
    Logger logger = LogManager.getLogger(SpringTestController.class);

/*    @Resource(name="jRubyTest")
    private ISimpleTestJRuby simpleTestJRuby;*/

    @ModelAttribute("user")
    public String addUser(String users){
        users="zhangefeng";
        return "zhangSanFeng";
    }

    @RequestMapping(value = "test",produces = "application/json")
    public String springTest(HttpSession session,@RequestHeader("host") String host){

        System.out.println(session.getAttribute("testUser"));
        System.out.println(host);
        return "test/test";
    }

    @RequestMapping("testSession")
    public String springTestSession(Model m){
        logger.trace("trace");
        logger.debug("debug");
        logger.info("info");
        logger.warn("warn");
        logger.error("error");
        logger.fatal("fatal");
       m.addAttribute("testUser","testsession");
       // testUser="zhangsanfeng";
        return "test/test";
    }


    /*@RequestMapping("testJRuby")
    public String testJRuby(HttpServletRequest request){
    double d1= simpleTestJRuby.calculate(200,300);
        System.out.println(d1);
        return "test/test";
    }*/

    @RequestMapping("testVoid")
    public void springTestSession(HttpServletRequest request,HttpServletResponse response) throws IOException {
        /*OutputStream os = response.getOutputStream();
        File file=new File("E:\\zhangsangtest.txt");
        InputStream in=new FileInputStream(file);
        try {
            response.reset();
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition", "attachment;fileName="
                    + "text.txt");
           // os.write();

            byte[] bytes=new byte[10];
            while(in.read(bytes)>0){
                os.write(bytes);
            }

            os.flush();
        } finally {
            if (os != null) {
                os.close();
            }
            if (in != null) {
                in.close();
            }
        }*/
        //  response
       PrintWriter p= response.getWriter();
        p.write("<html><head></head><body><form><input type='text'/></form></body></html>");
    }

}
